package tech.novastar.http;

import tech.novastar.service.StoreService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

/**
 * 库存删除，包含批量和行内删除操作
 */
@WebServlet("/StoreDeleteServlet")
public class StoreDeleteServlet extends HttpServlet {
    /**
     * 点击复选框时,通过form表单的post提交删除操作
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            Enumeration<String> data =  request.getParameterNames();
            StoreService storeService = new StoreService();
            while (data.hasMoreElements()) {
                int id = Integer.parseInt(data.nextElement());
                storeService.deleteById(id);
            }

            response.sendRedirect(request.getContextPath() + "/home");

    }

    /**
     * 点击当前行的删除操作时, 通过a标签发送
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        StoreService storeService = new StoreService();
        storeService.deleteById(id);
        response.sendRedirect(request.getContextPath() + "/home");
    }
}

